package de.midnightdance.server.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.ext.InvalidPasswordException;

import de.midnightdance.model.character.Bund;
import de.midnightdance.model.character.CharacterRequiemID;
import de.midnightdance.model.character.CharacterRequiemMensch;
import de.midnightdance.model.character.Clan;
import de.midnightdance.model.character.JagdTechnik;
import de.midnightdance.model.character.Ritual;
import de.midnightdance.model.character.disziplin.DisziplinDB;
import de.midnightdance.model.character.vorteil.VorteilDB;
import de.midnightdance.model.user.RequiemUser;

public class RequiemModel {
	
	private static RequiemModel instance; 
	
	public static RequiemModel getInstance() {
		if (instance == null) {
			instance = new RequiemModel();
		}
		return instance;
	}
	
	public RequiemUser isAuthenticUser(RequiemUser user) {
		RequiemUser result = null;
		try{
			ObjectContainer client = RequiemModelPersistance.getInstance().openUserClient(user);
			ObjectSet<RequiemUser> set = client.queryByExample(user);
			if(set.isEmpty()){
				result = null;
			}else{
				result = set.next();
				if(set.hasNext()){
					System.err.println("More than one User found!!");
				}
			}			
			RequiemModelPersistance.getInstance().closeUserClient(client);
		}catch(InvalidPasswordException exc){
			result = null;
		}		
		return result;
	}
	
	public RequiemUser[] getAllUsers(RequiemUser user) {
		List<RequiemUser> resultList = new ArrayList<RequiemUser>();
		//
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			//only return list, if user is permitted to use all users
			ObjectContainer client = RequiemModelPersistance.getInstance().openUserClient(user);
			ObjectSet<RequiemUser> set = client.query(RequiemUser.class);
			for (RequiemUser requiemUser : set) {
				resultList.add(requiemUser);
			}
		}		
		return (RequiemUser[]) resultList.toArray(new RequiemUser[resultList.size()]);
	}	
	
	public int addUser(RequiemUser usertoAdd, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().saveUser(usertoAdd, user)){
				return 0;
			}
		}		
		return -1;
	}	
	
	public int removeUser(RequiemUser userToRemove, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().deleteUser(userToRemove, user)){
				return 0;
			}
		}	
		return -1;
	}	

	public Collection<? extends CharacterRequiemID> getAllCharacters(RequiemUser user) {
		List<CharacterRequiemID> resultList = new ArrayList<CharacterRequiemID>();
		//
		if(user.isPermitted(RequiemUser.PERMISSION_READ_ALL)){
			ObjectContainer client = RequiemModelPersistance.getInstance().openClient(user);
			ObjectSet<CharacterRequiemID> set = client.query(CharacterRequiemID.class);
			for (CharacterRequiemID characterRequiemID : set) {
				resultList.add(characterRequiemID);
			}
		}		
		return resultList;
	}	
	
	public int addCharacter(CharacterRequiemMensch character, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_WRITE_ALL)){
			if(RequiemModelPersistance.getInstance().saveEntity(character, user)){
				return 0;
			}
		}		
		return -1;
	}	
	
	public int removeCharacter(CharacterRequiemMensch character, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_WRITE_ALL)){
			if(RequiemModelPersistance.getInstance().removeEntity(character, user)){
				return 0;
			}
		}		
		return -1;
	}	
	
	public VorteilDB[] getVorteile(RequiemUser user){
		ArrayList<VorteilDB> resultList = new ArrayList<VorteilDB>();
		//
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			ObjectContainer client = RequiemModelPersistance.getInstance().openClient(user);
			ObjectSet<VorteilDB> set = client.query(VorteilDB.class);
			for (VorteilDB vorteilDB : set) {
				resultList.add(vorteilDB);
			}
		}		
		return (VorteilDB[]) resultList.toArray(new VorteilDB[resultList.size()]);
	}
	
	public int addVorteil(VorteilDB vorteilDB, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().saveEntity(vorteilDB, user)){
				return 0;
			}
		}		
		return 0;
	}
	
	public int removeVorteil(VorteilDB vorteilDB, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().removeEntity(vorteilDB,user)){
				return 0;
			}
		}	
		return -1;
	}

	public Bund[] getBuende(RequiemUser user){
		ArrayList<Bund> resultList = new ArrayList<Bund>();
		//
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			ObjectContainer client = RequiemModelPersistance.getInstance().openClient(user);
			ObjectSet<Bund> set = client.query(Bund.class);
			for (Bund bund : set) {
				resultList.add(bund);
			}
		}		
		return (Bund[]) resultList.toArray(new Bund[resultList.size()]);
	}
	
	public int addBund(Bund bund, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().saveEntity(bund, user)){
				return 0;
			}
		}	
		return -1;
	}
	
	public int removeBund(Bund bund, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().removeEntity(bund,user)){
				return 0;
			}
		}		
		return -1;
	}
	
	public Clan[] getClane(RequiemUser user){
		ArrayList<Clan> resultList = new ArrayList<Clan>();
		//
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			ObjectContainer client = RequiemModelPersistance.getInstance().openClient(user);
			ObjectSet<Clan> set = client.query(Clan.class);
			for (Clan clan : set) {
				resultList.add(clan);
			}
		}		
		return (Clan[]) resultList.toArray(new Clan[resultList.size()]);
	}
	
	public int addClan(Clan clan, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().saveEntity(clan, user)){
				return 0;
			}
		}		
		return -1;
	}
	
	public int removeClan(Clan clan, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().removeEntity(clan,user)){
				return 0;
			}
		}		
		return -1;
	}
	
	public DisziplinDB[] getDisziplinen(RequiemUser user){
		ArrayList<DisziplinDB> resultList = new ArrayList<DisziplinDB>();		
		//
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			ObjectContainer client = RequiemModelPersistance.getInstance().openClient(user);
			ObjectSet<DisziplinDB> set = client.query(DisziplinDB.class);
			for (DisziplinDB diziplin : set) {
				resultList.add(diziplin);
			}
		}		
		return (DisziplinDB[]) resultList.toArray(new DisziplinDB[resultList.size()]);
	}
	
	public int addDisziplin(DisziplinDB disziplinDB, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().saveEntity(disziplinDB, user)){
				return 0;
			}
		}	
		return -1;
	}

	public int removeDisziplin(DisziplinDB disziplinDB, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().removeEntity(disziplinDB,user)){
				return 0;
			}
		}		
		return -1;
	}
	
	public Ritual[] getRituale(RequiemUser user){
		ArrayList<Ritual> resultList = new ArrayList<Ritual>();
		//
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			ObjectContainer client = RequiemModelPersistance.getInstance().openClient(user);
			ObjectSet<Ritual> set = client.query(Ritual.class);
			for (Ritual ritual : set) {
				resultList.add(ritual);
			}
		}		
		return (Ritual[]) resultList.toArray(new Ritual[resultList.size()]);
	}
	
	public int addRitual(Ritual ritual, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().saveEntity(ritual, user)){
				return 0;
			}
		}		
		return -1;
	}
	
	public int removeRitual(Ritual ritual, RequiemUser user){		
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().removeEntity(ritual,user)){
				return 0;
			}
		}		
		return -1;
	}
	
	public JagdTechnik[] getJagdtechniken(RequiemUser user){
		ArrayList<JagdTechnik> resultList = new ArrayList<JagdTechnik>();
		//
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			ObjectContainer client = RequiemModelPersistance.getInstance().openClient(user);
			ObjectSet<JagdTechnik> set = client.query(JagdTechnik.class);
			for (JagdTechnik jagdtechnik : set) {
				resultList.add(jagdtechnik);
			}
		}		
		return (JagdTechnik[]) resultList.toArray(new JagdTechnik[resultList.size()]);
	}
	
	public int addJagdTechnik(JagdTechnik jagdTechnik, RequiemUser user){
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().saveEntity(jagdTechnik, user)){
				return 0;		
			}
		}		
		return -1;
	}
	
	public int removeJagdTechnik(JagdTechnik jagdTechnik, RequiemUser user){		
		if(user.isPermitted(RequiemUser.PERMISSION_ADMIN)){
			if(RequiemModelPersistance.getInstance().removeEntity(jagdTechnik, user)){
				return 0;
			}
		}		
		return -1;
	}

	
}
